import Vue from 'vue'
import { download } from '@/utils/request'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import { getToken } from '@/utils/auth'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// import locale from 'element-ui/lib/locale/lang/en' // lang i18n
import echarts from 'echarts'
import '@/styles/index.scss' // global css
import '@/customIcons/icon.css' // 自定义icon
import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, handleTree } from '@/utils/ruoyi'
import App from './App.vue'
import store from './store'
import router from './router'
import plugins from './plugins' // plugins
import '@/icons' // icon
import '@/permission' // permission control
// 分页组件
import Pagination from '@/components/Pagination'
// 自定义表格工具组件
import RightToolbar from '@/components/RightToolbar'
import { initTIM, loginIM, eventlist, getUserId } from '@/tim/imUtils.js'
Vue.use(plugins)
Vue.component('Pagination', Pagination)
Vue.component('RightToolbar', RightToolbar)
/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
Vue.prototype.$echarts = echarts
// if (process.env.NODE_ENV === 'production') {
//   const { mockXHR } = require('../mock')
//   mockXHR()
// }

// set ElementUI lang to EN
// Vue.use(ElementUI, { locale })
// 如果想要中文版 element-ui，按如下方式声明
Vue.use(ElementUI, { size: 'small' })

Vue.config.productionTip = false

function initVm () {
  window._vm = new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
  })
}

function init () {
  const isLogin = !!getToken()
  if (!isLogin) {
    initVm()
    return
  }
  initTIM().then(res => {
    console.log(res)
    eventlist()
    console.log('登录的用户的id', getUserId())
    loginIM(getUserId()).then(res => {
      console.log(res)
      initVm()
    }).catch(e => {
      console.error(e)
      initVm()/* 不管im初始化失败还是成功 都得初始化vue*/
    })
  })
}

// 全局方法挂载

Vue.prototype.parseTime = parseTime
Vue.prototype.resetForm = resetForm
Vue.prototype.addDateRange = addDateRange
Vue.prototype.selectDictLabel = selectDictLabel
Vue.prototype.selectDictLabels = selectDictLabels
Vue.prototype.handleTree = handleTree
Vue.prototype.download = download
// 字典标签组件
import DictTag from '@/components/DictTag'
// 全局组件挂载
Vue.component('DictTag', DictTag)
init()
